package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.luoao.utils.PageEntity;
import com.api.gbhg.luoao.utils.VersionQuery;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
 * 事业单位在职人员工资津贴补贴经费需求版本表查询
 */
public class CauseWageAllowanceAction {
    @POST
    @Path("/getCauseWageAllowance")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String CauseWageAllowance(Map<String,String> param){
        PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
        String requestid = param.get("requestId");
            //查询版本主表
            RecordSet version = VersionQuery.getversion("14", "uf_sydwzzrygzjtbtjfbbb",requestid);
            Map<String, Object>[] map = new HashMap[version.getArray().size()];
            int i = 0;
            while (version.next()) {

                map[i] = new HashMap<>();
                String xmmin = version.getString("xmmin");
                String subcompanyname = version.getString("subcompanyname");
                String departmentmart = version.getString("departmentmark");
                int jbr = version.getInt("jbr");//经办人id
                int id = version.getInt("id");//id
                map[i].put("lcjlsqjlid", version.getInt("lcjlsqjlid"));//流程id
                map[i].put("xmmin", xmmin);//合同名称
                map[i].put("sbdw", subcompanyname);//申报单位
                map[i].put("sbks", departmentmart);//部门

                BigDecimal yszje = new BigDecimal(version.getString("yszje"));
                map[i].put("yszje", yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//总金额

                RecordSet userNameById = rmcd.getUserNameById(jbr);
                userNameById.next();
                String loginid = userNameById.getString("loginid");
                if (loginid == "") {
                    loginid = "系统管理员";
                }
                map[i].put("jbr", loginid);//经办人

                //查询明细表1
                RecordSet getdetail = VersionQuery.getdetail("uf_sydwzzrygzjtbtjfbbb_dt1", id);

                int ygzffrs = 0;
                double jxgz =0.00;
                double gzxjt =0.00;
                double shxbt =0.00;
                double ggxbt =0.00;
                double xjgz =0.00;
                while (getdetail.next()) {
                    xjgz =getdetail.getDouble("xjgz") + xjgz;//薪级工资
                    ygzffrs = getdetail.getInt("ygzffrs") + ygzffrs;//工资发放人数
                    jxgz =getdetail.getDouble("jxgz") + jxgz;//绩效工资
                    gzxjt =getdetail.getDouble("gzxjt") + gzxjt;//工作性津贴
                    shxbt =getdetail.getDouble("shxbt") + shxbt;//生活性补贴
                    ggxbt =getdetail.getDouble("ggxbt") + ggxbt;//改革性补贴
                }
                map[i].put("xjgz", FormatUtil.DmFormat(xjgz));
                map[i].put("jxgz", FormatUtil.DmFormat(jxgz));
                map[i].put("gzxjt", FormatUtil.DmFormat(gzxjt));
                map[i].put("shxbt", FormatUtil.DmFormat(shxbt));
                map[i].put("ggxbt", FormatUtil.DmFormat(ggxbt));
                map[i].put("ygzffrs", ygzffrs);

                i++;
            }
        PageEntity page = new PageEntity();
        page.setList(map);
        page.setFromtable("uf_sydwzzrygzjtbtjfbbb");

        return JSON.toJSONString(page);
    }
    //14 事业单位在职人员工资津贴补贴经费需求表
    public String CauseWageAllowance1(String requestid){
        PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
//        String requestid = param.get("requestId");
        //查询版本主表
        RecordSet version = VersionQuery.getversion("14", "uf_sydwzzrygzjtbtjfbbb",requestid);
        Map<String, Object>[] map = new HashMap[version.getArray().size()];
        int i = 0;
        while (version.next()) {

            map[i] = new HashMap<>();
            String xmmin = version.getString("xmmin");
            String subcompanyname = version.getString("subcompanyname");
            String departmentmart = version.getString("departmentmark");
            int jbr = version.getInt("jbr");//经办人id
            int id = version.getInt("id");//id
            map[i].put("lcjlsqjlid", version.getInt("lcjlsqjlid"));//流程id
            map[i].put("xmmin", xmmin);//合同名称
            map[i].put("sbdw", subcompanyname);//申报单位
            map[i].put("sbks", departmentmart);//部门


            BigDecimal yszje = new BigDecimal(version.getString("yszje"));
            map[i].put("yszje", yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//总金额


            RecordSet userNameById = rmcd.getUserNameById(jbr);
            userNameById.next();
            String lastname = userNameById.getString("lastname");
            if (lastname == "") {
                lastname = "系统管理员";
            }
            map[i].put("jbr", lastname);//经办人

            //查询明细表1
            RecordSet getdetail = VersionQuery.getdetail("uf_sydwzzrygzjtbtjfbbb_dt1", id);

            int ygzffrs = 0;
            double jxgz =0.00;
            double gzxjt =0.00;
            double shxbt =0.00;
            double ggxbt =0.00;
            double xjgz =0.00;
            while (getdetail.next()) {
                xjgz =getdetail.getDouble("xjgz") + xjgz;//薪级工资
                ygzffrs = getdetail.getInt("ygzffrs") + ygzffrs;//工资发放人数
                jxgz =getdetail.getDouble("jxgz") + jxgz;//绩效工资
                gzxjt =getdetail.getDouble("gzxjt") + gzxjt;//工作性津贴
                shxbt =getdetail.getDouble("shxbt") + shxbt;//生活性补贴
                ggxbt =getdetail.getDouble("ggxbt") + ggxbt;//改革性补贴
            }
            map[i].put("xjgz", new BigDecimal(FormatUtil.DmFormat(xjgz)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP) );
            map[i].put("jxgz", new BigDecimal(FormatUtil.DmFormat(jxgz)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP) );
            map[i].put("gzxjt", new BigDecimal(FormatUtil.DmFormat(gzxjt)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("shxbt", new BigDecimal(FormatUtil.DmFormat(shxbt)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("ggxbt", new BigDecimal(FormatUtil.DmFormat(ggxbt)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("ygzffrs", ygzffrs);

            i++;
        }
        PageEntity page = new PageEntity();
        page.setList(map);
        page.setFromtable("uf_sydwzzrygzjtbtjfbbb");

        return JSON.toJSONString(page);
    }

}
